package com.mszl.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mszl.blog.entity.Category;
import com.mszl.blog.entity.Tag;
import com.mszl.blog.dao.TagDao;
import com.mszl.blog.service.TagService;
import com.mszl.blog.vo.Result;
import com.mszl.blog.vo.TagVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * (Tag)表服务实现类
 *
 * @author makejava
 * @since 2025-08-02 22:03:42
 */
@Service("tagService")
public class TagServiceImpl implements TagService {
    @Resource
    private TagDao tagDao;

//    通过articleId找tags
@Override
    public List<TagVo> findTagsByArticleId(long articleId) {

        List<Tag> tags=tagDao.findTagsByArticleId(articleId);
        return copyList(tags);
    }

    private List<TagVo> copyList(List<Tag> taglist){
        List<TagVo> tagVoList=new ArrayList<>();
        for (Tag tag:taglist ) {
            tagVoList.add(copy(tag));
        }
        return  tagVoList;
    }


    private TagVo copy(Tag tag) {
        TagVo tagVo = new TagVo();
        BeanUtils.copyProperties(tag,tagVo);
        return  tagVo;

    }

    @Override
    public Tag queryById(Long id) {
        return this.tagDao.queryById(id);
    }

    @Override
    public Page<Tag> queryByPage(Tag tag, PageRequest pageRequest) {
        long total = this.tagDao.count(tag);
        return new PageImpl<>(this.tagDao.queryAllByLimit(tag, pageRequest), pageRequest, total);
    }

    @Override
    public Tag insert(Tag tag) {
        this.tagDao.insert(tag);
        return tag;
    }

    @Override
    public Tag update(Tag tag) {
        this.tagDao.update(tag);
        return this.queryById(tag.getId());
    }

    @Override
    public boolean deleteById(Long id) {
        return this.tagDao.deleteById(id) > 0;
    }

    @Override
    public Result hots(int limit) {
        /**
         1.tag have the most article
         2.tag_id 分类，count sort
         */
        List<Long>tagIds= tagDao.findHotsTagIds(limit);
        if(CollectionUtils.isEmpty(tagIds)){
            return Result.success(Collections.emptyList());
        }
       List<Tag> tagList=new ArrayList<>();
       for ( long tagId:tagIds){
           tagList.add(tagDao.queryById(tagId));
       }
       return Result.success(tagList);
    }

    /**
     * 查询文章所有标签
     * @return
     */

    @Override
    public Result findAll() {
        List<Tag> tagList=tagDao.selectList(new LambdaQueryWrapper<>());
        return Result.success(copyList(tagList));

    }

    @Override
    public Result findADetailById(Long id) {
        Tag tag=tagDao.selectById(id);
        return Result.success(tag);
    }
}
